package com.example.springaop_2.config;

import com.example.springaop_2.model.UserInfo;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date:2022-11-03
 * Time:14:47
 *
 * @Author: 东莞呵呵
 */
@Component
public class LoginInterrupt implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session=request.getSession(false);
        if( session != null && session.getAttribute("userinfo") != null){
            return true;
        }else {
            response.setStatus(401);
            response.sendRedirect("/login.html");
            return false;
        }
    }
}
